/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
/*
 * Created on 3 août 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package fr.jade.util.log.handler;

import java.io.File;
import java.io.PrintStream;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * @author Sylvain Sicard
 */
public class FileHandler implements Handler{

	private PrintStream ps;
	
	public void start(){
		try{
		  // TODO use a PrintStream
			File f = new File("/tmp/ssicard-jade-logs.txt");
			f.createNewFile();
			ps = new PrintStream(f);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void stop(){
		try{
			ps.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void log(String msg) {
		try{
			GregorianCalendar cal = new GregorianCalendar();
			ps.print("~"+cal.get(Calendar.HOUR)+":"+cal.get(Calendar.MINUTE)+":"+cal.get(Calendar.SECOND));
			ps.print("~"+cal.getTimeInMillis()+"~");
			ps.print(msg+"\n");
			ps.flush();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
  @Override
  public PrintStream getPrintStream() {
    return ps;
  }
	
}
